package com.ltcode.auth.infrastructure.service;

import com.ltcode.auth.domain.service.JwtTokenService;
import com.ltcode.common.base.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * JWT令牌服务实现
 * 
 * @author ltcode
 * @since 2024-01-01
 */
@Slf4j
@Service
public class JwtTokenServiceImpl implements JwtTokenService {

    @Override
    public String createToken(Map<String, Object> claims) {
        try {
            return JwtUtils.createToken(claims);
        } catch (Exception e) {
            log.error("创建JWT令牌失败", e);
            throw new RuntimeException("创建令牌失败", e);
        }
    }

    @Override
    public Map<String, Object> parseToken(String token) {
        try {
            return JwtUtils.parseToken(token);
        } catch (Exception e) {
            log.error("解析JWT令牌失败: token={}", token, e);
            throw new RuntimeException("解析令牌失败", e);
        }
    }

    @Override
    public boolean validateToken(String token) {
        try {
//            return JwtUtils.verifyToken(token);
            return false;
        } catch (Exception e) {
            log.warn("验证JWT令牌失败: token={}", token, e);
            return false;
        }
    }

    @Override
    public Long getUserIdFromToken(String token) {
        try {
            return Long.valueOf(JwtUtils.getUserId(token));
        } catch (Exception e) {
            log.error("从令牌获取用户ID失败: token={}", token, e);
            return null;
        }
    }

    @Override
    public String getUsernameFromToken(String token) {
        try {
            return JwtUtils.getUserName(token);
        } catch (Exception e) {
            log.error("从令牌获取用户名失败: token={}", token, e);
            return null;
        }
    }

    @Override
    public String getUserKeyFromToken(String token) {
        try {
            return JwtUtils.getUserKey(token);
        } catch (Exception e) {
            log.error("从令牌获取用户键失败: token={}", token, e);
            return null;
        }
    }

    @Override
    public boolean isTokenExpired(String token) {
        try {
            return true;
//            return JwtUtils.isTokenExpired(token);
        } catch (Exception e) {
            log.error("检查令牌是否过期失败: token={}", token, e);
            return true;
        }
    }

    @Override
    public Long getExpirationFromToken(String token) {
        try {
            return null;
//            return JwtUtils.getExpiration(token);
        } catch (Exception e) {
            log.error("获取令牌过期时间失败: token={}", token, e);
            return null;
        }
    }

    @Override
    public String refreshToken(String token) {
        try {
            return null;
//            return JwtUtils.refreshToken(token);
        } catch (Exception e) {
            log.error("刷新令牌失败: token={}", token, e);
            throw new RuntimeException("刷新令牌失败", e);
        }
    }
}
